<?php

namespace App\Admin\Actions\Post;

use App\Models\CollPosition;
use App\Models\CompanyJob;
use Encore\Admin\Actions\RowAction;
use Illuminate\Database\Eloquent\Model;

class DeletePosition extends RowAction
{
    public $name = '删除';

    public function handle(Model $model)
    {
        $values = CollPosition::where('pid', $model->cid)->first();
        if ($values) {
            return $this->response()->error('该工种下有子工种，暂不可删除！')->refresh();
        } else {
            $jobs = CompanyJob::where('job_category1', $model->cid)->count();
            if ($jobs > 0) {
                return $this->response()->error('该工种下有项目，暂不可删除！')->refresh();
            }
        }

        CollPosition::destroy($model->id);

        return $this->response()->success('Success message.')->refresh();
    }

    public function dialog()
    {
        $this->confirm('确定删除该工种吗？');
    }

}